package com.maori.languageapp.consts;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CaptureFilterAdapter {

	/*
	 * _id;LID;NID,IdiomID ;Idiom ;PhraseID ;Phrase ;SongID ;song_title
	 * ;song_content ;StoryID ; story_title ;story_content ;WordID ;word
	 */

	public static final String KEY_ID = "_id";
	public static final String KEY_LID = "LID";
	public static final String KEY_NID = "NID";
	public static final String KEY_IdiomID = "IdiomID";
	public static final String KEY_Idiom = "Idiom";
	public static final String KEY_PhraseID = "PhraseID";
	public static final String KEY_Phrase = "Phrase";

	public static final String KEY_SongID = "SongID";
	public static final String KEY_song_title = "song_title";
	public static final String KEY_song_content = "song_content";
	public static final String KEY_StoryID = "StoryID";
	public static final String KEY_story_title = "story_title";
	public static final String KEY_story_content = "story_content";
	public static final String KEY_WordID = "Otherwords";
	public static final String KEY_word = "word";
	public static final String KEY_Word_Maori = "Otherwords";

	public static final String KEY_ENG_TITLE = "english_title";
	public static final String KEY_ENG_DET = "english_det";
	public static final String KEY_MAO_TITLE = "maori_title";
	public static final String KEY_MAO_DET = "maori_det";
	public static final String KEY_CATE = "category";

	private static final String TAG = "CaptureFilterAdapter";
	private DatabaseHelper mDbHelperItems;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "maorilanguageapp.db";
	private String BOOKMARK_TABLE = "Bookmark";
	private String PHRASE_TABLE = "Phrase";
	private String SONG_TABLE = "Song";
	private String STORY_TABLE = "Story";
	private String WORD_TABLE = "Word";
	private String IDIOM_TABLE = "Idiom";
	private static final int DATABASE_VERSION = 1;

	private final Context mCtx;
	int c;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// Log.w(TAG, DATABASE_CREATE);
			// db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		}
	}

	public CaptureFilterAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public CaptureFilterAdapter open() throws SQLException {
		mDbHelperItems = new DatabaseHelper(mCtx);
		mDb = mDbHelperItems.getWritableDatabase();
		return this;
	}

	public void close() {
		if (mDbHelperItems != null) {
			mDbHelperItems.close();
		}
	}

	// ******Fetch All Search List Items***********
	// Filter Words Details (1)
	public Cursor fetchItemsByName(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(WORD_TABLE, new String[] { KEY_ID, KEY_LID,
					KEY_NID, KEY_WordID, KEY_word }, null, null, null, null,
					null);

		} else {
			mCursor = mDb.query(true, WORD_TABLE, new String[] { KEY_ID,
					KEY_LID, KEY_NID, KEY_WordID, KEY_word }, KEY_word
					+ " like '%" + inputText + "%'", null, null, null, null,
					null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	// Filter Words Details (2)
	public Cursor fetchPhraseItemsByName(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(PHRASE_TABLE, new String[] { KEY_ID, KEY_LID,
					KEY_NID, KEY_PhraseID, KEY_Phrase }, null, null, null,
					null, null);

		} else {
			mCursor = mDb.query(true, PHRASE_TABLE, new String[] { KEY_ID,
					KEY_LID, KEY_NID, KEY_PhraseID, KEY_Phrase }, KEY_word
					+ " like '%" + inputText + "%'", null, null, null, null,
					null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	// ******Fetch All List Items***********
	// Fetch Words Details (1)
	public Cursor fetchAllItems() {
		c = c + 1;
		Log.v(TAG, "DJ-Filter Words :" + c);
		Cursor mCursor = mDb.query(WORD_TABLE, new String[] { KEY_ID, KEY_LID,
				KEY_NID, KEY_WordID, KEY_word }, null, null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	// Fetch Phrase Details (2)
	public Cursor fetchAllPhraseItems() {
		c = c + 1;
		Log.v(TAG, "DJ-Filter Words :" + c);
		Cursor mCursor = mDb.query(PHRASE_TABLE, new String[] { KEY_ID,
				KEY_LID, KEY_NID, KEY_PhraseID, KEY_Phrase }, null, null, null,
				null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	// Fetch Story Details (2)
	public Cursor fetchAllStoryItems() {
		c = c + 1;
		Log.v(TAG, "DJ-Filter Words :" + c);
		Cursor mCursor = mDb.query(STORY_TABLE, new String[] { KEY_ID, KEY_LID,
				KEY_NID, KEY_StoryID, KEY_story_title, KEY_story_content },
				null, null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	// Fetch Idiom Details (2)
	public Cursor fetchAllIdiomItems() {
		c = c + 1;
		Log.v(TAG, "DJ-Filter Words :" + c);
		Cursor mCursor = mDb
				.query(IDIOM_TABLE, new String[] { KEY_ID, KEY_LID, KEY_NID,
						KEY_IdiomID, KEY_Idiom }, null, null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	// Fetch Song Details (2)
	public Cursor fetchAllSongItems() {
		c = c + 1;
		Log.v(TAG, "DJ-Filter Words :" + c);
		Cursor mCursor = mDb.query(SONG_TABLE, new String[] { KEY_ID, KEY_LID,
				KEY_NID, KEY_SongID, KEY_song_title, KEY_song_content }, null,
				null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	/********** Fetch All BookMarks **************/
	// Fetch Words Details (1)
	public Cursor fetchAllBookMarkItems() {
		c = c + 1;
		Log.v(TAG, "DJ-Filter Words :" + c);
		Cursor mCursor = mDb.query(BOOKMARK_TABLE, new String[] { KEY_ID,
				KEY_NID, KEY_ENG_TITLE, KEY_ENG_DET, KEY_MAO_TITLE,
				KEY_MAO_DET, KEY_CATE }, null, null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	// Filter Words Details (1)
	public Cursor fetchBookMarkItemsByName(String inputText)
			throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(BOOKMARK_TABLE, new String[] { KEY_ID, KEY_NID,
					KEY_ENG_TITLE, KEY_ENG_DET, KEY_MAO_TITLE, KEY_MAO_DET,
					KEY_CATE }, null, null, null, null, null);

		} else {
			mCursor = mDb.query(true, BOOKMARK_TABLE, new String[] { KEY_ID,
					KEY_NID, KEY_ENG_TITLE, KEY_ENG_DET, KEY_MAO_TITLE,
					KEY_MAO_DET, KEY_CATE }, KEY_MAO_TITLE + " like '%"
					+ inputText + "%'", null, null, null, null, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

}
